package com.charactereducation.admin.controller.user;

import com.charactereducation.admin.service.AdminUserService;
import com.charactereducation.admin.service.MenuService;
import com.charactereducation.admin.service.RoleService;
import com.charactereducation.module.AdminMenu;
import com.charactereducation.module.AdminUser;
import com.charactereducation.module.Role;
import com.charactereducation.tools.MD5Tools;
import com.charactereducation.tools.def.Constant;
import com.charactereducation.tools.def.MessageConstant;
import com.charactereducation.tools.myRunTime.MyRunTimeExcption;
import com.charactereducation.tools.result.ResultObj;
import com.charactereducation.tools.result.ReturnCode;
import com.charactereducation.webcore.controller.BaseController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/2/26.
 */
@RestController
@RequestMapping(value = "/admin/user")
public class AdminUserController extends BaseController {
    /*@Autowired
    RedisCache redisCache;*/
    @Autowired
    RoleService roleService;
    @Autowired
    MenuService menuService;
    @Autowired
    AdminUserService adminUserService;

    @RequestMapping(value = "/showMenuList")
    public ModelAndView showMenuList() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("admin/menu/getMenuList");

        Map<String, String> map = this.showRequestParameters();
        map.put("menuParentId", "0");
        List<AdminMenu> menuList = menuService.getMenuList(map);

        mov.addObject("menuList", menuList);

        mov.addObject("bean", map);
        return mov;

    }

    @RequestMapping(value = "/getMenuList", method = RequestMethod.POST)
    @ResponseBody
    public String getMenuList() {

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters();

            List<AdminMenu> menuList = menuService.getMenuList(map);

            resultObj.setData(menuList);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();

    }




    /**
     * 跳转到修改页面
     *
     * @return
     */
    @RequestMapping(value = "/updatePassWord")
    @ResponseBody
    public ModelAndView updatePassWord() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/admin/user/updatePassWord");
        return mov;
    }

    /**
     * 用户列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/getUserList")
    public ModelAndView getUserList(Integer page, Integer pageSize) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/admin/user/userList");

        if (page == null || page == 0) {
            page = Constant.PAGENUM;
        }
        // 设置每页记录数
        if (pageSize == null || pageSize == 0) {
            pageSize = Constant.PAGESIZENum;
        }
        PageHelper.startPage(page, pageSize);
        Map<String, String> map = this.showRequestParameters();
        List<AdminUser> userList = adminUserService.getUserList(map);

        PageInfo<AdminUser> pageinfo = new PageInfo<AdminUser>(userList);
        mov.addObject("pageinfo", pageinfo);
        mov.addObject("bean", map);
        return mov;

    }

    @RequestMapping(value = "/getUser")
    public ModelAndView getUser(Long tid) {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("/admin/user/adminUserAdd");
        List<Role> roleList = roleService.getRoleList(null);
        mov.addObject("roleList", roleList);
        try {
            if (tid != null) {
                AdminUser adminUser = adminUserService.getUser(new HashMap<String, String>() {
                    {
                        put("tid", String.valueOf(tid));
                    }
                });
                mov.addObject("adminUser", adminUser);

            }

        } catch (MyRunTimeExcption e) {
            e.printStackTrace();
        }
        return mov;

    }

    /**
     * 修改后台用户信息
     *
     * @return
     */
    @RequestMapping(value = "/modifyUser", method = RequestMethod.POST)
    @ResponseBody
    public String modifyUser(AdminUser adminUser) {

        ResultObj resultObj = new ResultObj();
        try {
            adminUserService.modifyUser(adminUser);
            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping(value = "/modifyPassword", method = RequestMethod.POST)
    @ResponseBody
    public String modifyPassword() {

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters();

            AdminUser adminUser = adminUserService.selectAdminById(map);
            String PageOldPassWord = MD5Tools.MD5(map.get("oldPassWord"));
            String OldPassWord = adminUser.getPassWord();

            if (PageOldPassWord.equals(OldPassWord)) {
                adminUserService.modifyUser(adminUser);
                resultObj.setMsg(MessageConstant.MODIFY_PASSWORD_SUCCESS);
            } else {
                resultObj.setCode(ReturnCode.FAIL.getCode());
                resultObj.setMsg(MessageConstant.PASSWORD_ERRO);
            }

        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();

    }

    /**
     * 重置密码
     *
     * @return
     */
    @RequestMapping(value = "/resetPassword")
    @ResponseBody
    public String resetPassword() {
        System.out.println("=================================resetPassword==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters();
            map.put("passWord", "123456");
            AdminUser adminUser = adminUserService.selectAdminById(map);
            adminUserService.modifyUser(adminUser);
            resultObj.setMsg(MessageConstant.PASSWORD_RESET_SUCCESS);
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }

        return resultObj.toJson();

    }

    @RequestMapping(value = "/loginOut", method = RequestMethod.POST)
    @ResponseBody
    public String loginOut() {
        System.out.println("===============================loginOut============================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            request.getSession().invalidate();
        } catch (MyRunTimeExcption myRunTimeExcption) {
            myRunTimeExcption.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(myRunTimeExcption.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj.toJson();
    }
}
